package com.hu.jie.mywallpager.fragment;


import android.app.Activity;
import android.app.Service;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.Response;
import com.hu.jie.mywallpager.R;
import com.hu.jie.mywallpager.apadter.MySeekAdapter;
import com.hu.jie.mywallpager.apadter.SeekAdapter;
import com.hu.jie.mywallpager.bean.JsonList;
import com.hu.jie.mywallpager.bean.Seek;
import com.hu.jie.mywallpager.bean.SeekList;
import com.hu.jie.mywallpager.contanse.UrlString;
import com.hu.jie.mywallpager.utils.VolleyUtils;
import com.hu.jie.mywallpager.view.SearchBannerView;
import com.hu.jie.mywallpager.view.SearchTopView;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class SeekFragment extends Fragment {
    private ListView lv;
    private Activity mActivity;
    private List<JsonList.FirstInnerBean> mDatas;
    private SeekAdapter adapter;
    private SearchTopView toopview;
    private SearchBannerView bannerview;


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mActivity = getActivity();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_seek, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        lv = (ListView) view.findViewById(R.id.listseek);
        final LinearLayout layout = (LinearLayout) view.findViewById(R.id.action_bar_containertoolbar);
        final Toolbar toolbar = (Toolbar) view.findViewById(R.id.scrolltoolbar);
        mDatas = new ArrayList<>();
        adapter = new SeekAdapter(mActivity, mDatas);

        //添加头部
        toopview = new SearchTopView(mActivity);
        bannerview = new SearchBannerView(mActivity);
        lv.addHeaderView(toopview);
        lv.addHeaderView(bannerview);
        lv.setAdapter(adapter);

        layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //弹出浮动框
                //封装代码
                showPopUpWindow(layout, toolbar);
                //弹出输入法
                //输入法系统
                InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Service.INPUT_METHOD_SERVICE);
                //如果当前没有显示软输入法键盘可以显示，显示的flag,隐藏的flag
                imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_IMPLICIT_ONLY);
            }
        });
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initData();
    }

    private void initData() {
        VolleyUtils.newInstance(getContext()).newGsonRequest(Request.Method.GET, UrlString.URL_SEEK, new Response.Listener<Seek>() {
            @Override
            public void onResponse(Seek response) {
                toopview.setData(response.data);
            }
        }, null, Seek.class);
        VolleyUtils.newInstance(getContext()).newGsonRequest(Request.Method.GET, UrlString.URL_SUB, new Response.Listener<SeekList>() {
            @Override
            public void onResponse(SeekList response) {
                bannerview.setDatas(response.data.Topic);
            }
        }, null, SeekList.class);
        VolleyUtils.newInstance(getContext()).newGsonRequest(Request.Method.GET, UrlString.URL_JSON, new Response.Listener<JsonList>() {
            @Override
            public void onResponse(JsonList response) {
                mDatas.addAll(response.data);
                adapter.notifyDataSetChanged();
            }
        }, null, JsonList.class);
    }


    //窗口级别的，由WindowManager统一管理(浮动窗口)
    private PopupWindow popupwindow;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        popupwindow = new PopupWindow();
    }

    private void showPopUpWindow(LinearLayout layout, Toolbar toolbar) {
        //判断当前popupwindow没有显示
        if (!popupwindow.isShowing()) {
            //设置popupwindow的属性
            popupwindow.setWidth(toolbar.getWidth());
            popupwindow.setHeight(toolbar.getHeight());
            //设置popupwindow自己的视图
            //自定义layout.xml通过填充器转换为View就可以了
            View v = getActivity().getLayoutInflater().inflate(R.layout.popupwindow, null);
            TextView tv = (TextView) v.findViewById(R.id.text5);
            tv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //人为取消浮动窗口
                    popupwindow.dismiss();
                }
            });
            popupwindow.setContentView(v);
            popupwindow.setFocusable(true);//设置popupwindow获取焦点
            //可以点击popupwindow的外部，点击以后可以消除popupwindow
            popupwindow.setOutsideTouchable(true);
            //一般情况下可以进行偏移的操作
            popupwindow.showAtLocation(layout, Gravity.TOP, 0, getStatusHeight());
        }
    }

    //获取状态栏的高度
    public int getStatusHeight() {
        int height = 0;
        int resId;//获取状态栏变量对应的地址
        //通过变量名找地址，通过地址取地址的内容
        resId = getResources().getIdentifier("status_height", "dimen", "android");
        if (resId > 0) {
            //通过地址取高度
            height = getResources().getDimensionPixelOffset(resId);
        }
        Log.i("--------------------->", "状态栏的高度" + height);
        return height;
    }


}

















